import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-02-02
 * Time:22:54
 */
public class MinStack {
    public Stack<Integer> stack = new Stack<>();
    public Stack<Integer> minstack = new Stack<>();

    public MinStack() {

    }

    public void push(int val) {
        stack.push(val);
        if(minstack.isEmpty()) {
            minstack.push(val);
        } else {
            int top = minstack.peek(); //取minstack栈顶值与val比较
            //注意这里要取到等号
            if(val <= top) {
                minstack.push(val);
            }
        }
    }

    public void pop() {

        if(stack.isEmpty()) {
            return;
        }
        //出stack，比较出栈的值是否和minstack栈顶值相等，若相等，那minstack也要出栈
        int pop = stack.pop();
        if(pop == minstack.peek()) {
            minstack.pop();
        }
    }

    public int top() {
        if(stack.isEmpty()) {
            return -1;
        } else {
            return stack.peek();
        }
    }

    public int getMin() {
        if(minstack.isEmpty()) {
            return -1;
        } else {
            return minstack.peek();
        }
    }

}
